import { observable, action } from 'mobx';
import { ListViewPage } from 'weaver-mobile-page';
import * as Api from '../../api/BlogAttentionApi'; 

const shareToMeParams = {
  shareToMeCurrentPage: 1,
  seachValue: '',
  noData: false,
}
export default class ShareToMeStore {
  @observable shareToMeList = new ListViewPage.Store({ route:'/userCenter/blogAttention/share', id:'share' });
  @observable shareToMeCurrentPage = shareToMeParams.shareToMeCurrentPage;
  @observable seachValue = shareToMeParams.seachValue;
  @observable noData = shareToMeParams.noData;

  @action setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }

  @action('分享给我列表请求')
  getMyAttentionShare = () => {
    this.shareToMeList.setState({loading: true});
    let seachParams = { searchUserName: this.seachValue }
    let params = {
      currentPage: this.shareToMeCurrentPage++,
      pageSize: 20,
      type:'share',
      ...seachParams,
    }
    Api.getMyAttentionShare(params).then(data=>{
      const { status, attentionList, currentPage, pageSize, total } = data;
      if (attentionList && attentionList.length === 0) {
        this.noData = true;
      }else{
          this.noData = false;
      }
      if (status === '1') {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: total > currentPage * pageSize,
          data: [...(currentPage === 1 ? [] : this.shareToMeList.data), ...attentionList],
        };

        this.shareToMeList.setState(newState);
        this.shareToMeList.updateDataSource();
      }
    })
  }

  @action('请求下一页数据')
  getShareToMeListNextPage = () => {
    this.getMyAttentionShare();
  }

  @action('下拉刷新')
  getShareToMeListRefresh = () => {
    this.shareToMeCurrentPage = 1;
    this.getMyAttentionShare();
  }

  @action('关注状态更新')
  updateUserAttentionStatus = (attnInfo) =>{
    let {userId,attentionStatus,specialStatus} = attnInfo;
		let attlist = this.shareToMeList.data.map(att=>{
      if (att.userId == userId)
      {
        if(specialStatus){
          return {...att, attentionStatus: attentionStatus,specialStatus:specialStatus};
        }

        return {...att, attentionStatus: attentionStatus};
      }


      return att;
		});
    this.shareToMeList.setState({data: attlist});
    this.shareToMeList.updateDataSource();
  }

  @action('搜索查询')
  shareToMeSearch = (v) => {
    this.seachValue = v;
    this.shareToMeCurrentPage = 1;
    this.getMyAttentionShare();
  }

  @action
  shareToMeClear = () => this.setState(shareToMeParams);
}